package com.twentytwok.hospitalsystem22K.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.write.builder.ExcelWriterBuilder;
import com.alibaba.excel.write.builder.ExcelWriterSheetBuilder;
import com.twentytwok.hospitalsystem22K.bo.DepartmentBo;
import com.twentytwok.hospitalsystem22K.common.R;
import com.twentytwok.hospitalsystem22K.pojo.Department;
import com.twentytwok.hospitalsystem22K.service.DepartmentService;
import com.twentytwok.hospitalsystem22K.vo.DepartmentVo;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;


import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @Author le
 * @Date 2021/11/11
 */

@RestController
@RequestMapping("/api/department")
@CrossOrigin("*")
public class DepartmentController {
    @Autowired
    private DepartmentService departmentService;

    // 分页数据
    @RequestMapping("/departmentPageData")
    public R getDepartmentPageData(@RequestParam Map<String,Object> params) {
        return departmentService.selectByParams(params);
    }

    // 删除数据
    @RequestMapping("/deleteDepartmentData")
    public R deleteDepartmentData(Integer id) {
        return departmentService.deleteDepartmentData(id);
    }

    // 删除所选数据, 前端有点问题, 换方法了
    @RequestMapping("/deleteSelectedDepartmentData")
    public R deleteSelectedDepartmentData(List<Department> list) {
        return departmentService.deleteSelectedDepartmentData(list);
    }

    // 修改数据
    @RequestMapping("/updateDepartmentData")
    public R updateDepartmentData(Department department) {
        return departmentService.updateDepartmentData(department);
    }

    // 添加数据
    @RequestMapping("/addDepartmentData")
    public R addDepartmentData(Department department) {
        return departmentService.addDepartmentData(department);
    }

    // 上传图片
    @RequestMapping("/uploadDepartmentImg")
    public R uploadDepartmentImg(MultipartFile file, HttpServletRequest request) {
        return null;
    }

    // 柱形图
    @RequestMapping("/departmentHistogram")
    public DepartmentVo departmentHistogram() {
        return departmentService.selectDepartmentBarVo();
    }

    // 饼状图
    @RequestMapping("/departmentPieChart")
    public List<DepartmentBo> departmentPieChart() {
        return departmentService.selectDepartmentBarBo();
    }

    // 获取所有科室信息
    @RequestMapping("/getAllDepartment")
    public List<Department> getAllDepartment() {
        return departmentService.selectAllDepartment();
    }

    // 导出excel
    @RequestMapping("/exportExcel")
    public void exportExcel() {
        List<Department> departments = departmentService.selectAllDepartment();

        ExcelWriterBuilder writerBuilder = EasyExcel.write("test.xlsx", Department.class);

        ExcelWriterSheetBuilder sheet = writerBuilder.sheet();

        sheet.doWrite(departments);
    }
}
